package leetcode_day._2021._202104._1120;

import general_class.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/4/13 14:50
 * 二叉搜索树节点最小距离
 * 由于是二叉搜索数，找到最左边的两个数即可
 * 设置两个数，一个存前一个数，另一个存当前数
 * 最小值一定是响铃的两个数
 * 中序遍历
 * 添加两个值
 *
 */
public class _13_783 {
    public static void main(String[] args) {
        // [27,null,34,null,58,50,null,44]
        TreeNode root = new TreeNode(4, new TreeNode(2, new TreeNode(1, null, null), new TreeNode(3, null, null)),
                new TreeNode(6, null, null));
        System.out.println(new _13_783().minDiffInBST(root));
    }

    public int minDiffInBST(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        mot(root, list);
        int ans = Integer.MAX_VALUE;
        for (int i = 1; i < list.size(); i++) {
            ans = Math.min(list.get(i) - list.get(i - 1), ans);
        }
        return ans;
    }

    public void mot(TreeNode root, List<Integer> list) {
        if (null == root.left && null == root.right) {
            list.add(root.val);
            return;
        }
        if (null != root.left) mot(root.left, list);
        list.add(root.val);
        if (null != root.right) mot(root.right, list);
    }
}
